﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MessMain
{
    public partial class Comunication : Form
    {
        public Client cl;
        public string myName;

        private string serverIP;
        private int serverPort;
        private string limba;
        
        public Comunication(string serverIP, string serverPort, string limba)
        {
            InitializeComponent();
            Rename(limba);

            this.serverIP = serverIP;
            this.serverPort = Int32.Parse(serverPort);
            this.limba = limba;

            run();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Hide the form...
           // MessageBox.Show("La Revedere");
            System.Environment.Exit(0);
            // Cancel the close...
            e.Cancel = true;
        }

        private bool tabCtl2(string g)
        {
            for (int i = 0; i < this.tabControl2.Controls.Count; i++)
            {
                if (this.tabControl2.Controls[i].Text.Equals(g))
                {
                    return true;
                }
            }
            return false;
        }

        private TabItems getTabList(string g)
        {
            TabItems us = null;
            for (int i = 0; i < tablist.Count; i++)
            {
                if (tablist[i].Text.Equals(g))
                {
                    us = tablist[i];
                }
            }
            return us;
        }

        private void users_DoubleClick(object sender, EventArgs e)
        {
            if (this.users.SelectedIndex != -1)
            {
                if (!tabCtl2((string)this.users.SelectedItem))
                {
                    TabItems itm;
                    if ((itm = getTabList((string)this.users.SelectedItem)) != null)
                    {
                        setCtlTab2(itm);
                    }
                    else
                    {
                        addUserInTabCtl((string)users.SelectedItem);
                    }
                }                
            }
        }

        public void addUserInTabCtl(string name)
        {
            tbi = new TabItems();
            tbi.c = this;
            tbi.myName = this.myName;
            tbi.addUser(name);
            tbi.upDateVar();
            tablist.Add(tbi);
            setCtlTab2(tbi);
        }
        public void setCtlTab2(TabItems tbi)
        {
            this.tabControl2.Controls.Add(tbi);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (this.text2.Text.Trim().Contains("changeMyName"))
            //{
            //    sendMsgTo(this.text2.Text.Trim());
            //    this.text2.ResetText();
            //}
            //else
            //if (users.SelectedIndex == -1)
            //{
            //    this.text1.Text = "Nu ati selectat nici un Client pentru a putea conversa";
            //    this.text1.Text += "\r\n";
            //    this.text2.ResetText();
            //}
            //else
            //{
            //    string g = this.text2.Text.Trim();
            //    //String tex = " " + myName + " : " + this.text2.Text.Trim();
            //    this.text2.ResetText();
            //    if (g.Trim().CompareTo("")!=0)
            //    {
            //        this.text1.Text += " ";
            //        this.text1.Text += myName;
            //        this.text1.Text += " : ";
            //        this.text1.Text += g;
            //        this.text1.Text += "\r\n";
            //        this.text1.SelectionStart = this.text1.TextLength;
            //        this.text1.ScrollToCaret();
            //        sendMsgTo(g);
            //    }
            //}
        }

        private void text2_TextChanged(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //if (e.KeyChar == 13)
            //{
            //    if (this.text2.Text.Trim().Contains("changeMyName"))
            //    {
            //        sendMsgTo(this.text2.Text.Trim());
            //        this.text2.ResetText();
            //    }else
            //    if (users.SelectedIndex == -1)
            //    {
            //        this.text1.Text = "Nu ati selectat nici un Client pentru a putea conversa";
            //        this.text1.Text += "\r\n";
            //        this.text2.ResetText();
            //    }
            //    else
            //    {
            //        string g = this.text2.Text.Trim();
            //        //String tex = " " + myName.ToString() + " : " + this.text2.Text.Trim();
            //        this.text2.ResetText();
            //        if (g.Trim().CompareTo("")!=0)
            //        {
            //            this.text1.Text += " ";
            //            this.text1.Text += myName;
            //            this.text1.Text += " : ";
            //            this.text1.Text += g;
            //            this.text1.Text += "\r\n";
            //            this.text1.SelectionStart = this.text1.TextLength;
            //            this.text1.ScrollToCaret();
            //            sendMsgTo(g);
            //        }
            //    }
            //}
        }

        //void sendMsgTo(string msg1)
        //{
        //    string msg = "";
        //    msg += "/msg ";
        //    msg += users.SelectedItem;
        //    msg += " ";
        //    msg += msg1;
        //    cl.putMsg(msg);
        //}

        private void users_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.text1.Text += this.users.SelectedIndex;
            //this.text1.Text += "\r\n";
        }

        public void run()
        {
            cl = new Client(serverIP, serverPort, this, limba);
            cl.init();

            Thread th = new Thread(new ThreadStart(cl.run));
            th.Start();

            UpDateList up = new UpDateList(cl);
            th = new Thread(new ThreadStart(up.run));
            th.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new CreareConf(this, limba);
                frm.Show();  
            }
           frm.setVisible(true);          
        }

        private void iesireC_Click(object sender, EventArgs e)
        {

        }

    }
}
